package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.CategoryWithChildBO;
import io.github.talelin.latticy.common.enumeration.CategoryRootOrNotEnum;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.CategoryDTO;
import io.github.talelin.latticy.model.CategoryDO;
import io.github.talelin.latticy.mapper.CategoryMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mall@Alice
 * @since 2020-11-26
 */
@Service
public class CategoryService extends ServiceImpl<CategoryMapper, CategoryDO>{

    @Autowired
    private CategoryMapper categoryMapper;

    public void update(CategoryDTO dto, Integer id) {
        CategoryDO category = this.getCategoryById(id);
        BeanUtils.copyProperties(dto,category);
        this.updateById(category);
    }

    public void detlete(Integer id) {
        CategoryDO category = this.getCategoryById(id);
        if(category.getIsRoot() == CategoryRootOrNotEnum.ROOT.getValue()) {
            // 查找当前父分类下有无子分类，如有则不能删除
            QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(CategoryDO::getParentId, id); // 父类=Id
            wrapper.lambda().eq(CategoryDO::getIsRoot, CategoryRootOrNotEnum.NOT_ROOT.getValue()); // is_root为0
            wrapper.last("limit 1");
            CategoryDO subCategory = this.baseMapper.selectOne(wrapper);
            if(subCategory != null) {
                throw new ForbiddenException();
            }
        }
        this.getBaseMapper().deleteById(id);
    }

    public CategoryDO getCategoryById(Integer id) {
        CategoryDO categoryDO = this.getById(id);
        if (categoryDO == null) {
            throw new NotFoundException(40000);
        }
        return categoryDO;
    }

    public IPage<CategoryDO> getCategoriesByPage(Integer count, Integer page, Integer root) {
        Page<CategoryDO> pager = new Page<>(page, count);
        QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(CategoryDO::getIsRoot, root);
        return this.getBaseMapper().selectPage(pager, wrapper);
    }

    public IPage<CategoryDO> getSubCategoriesByPage(Integer count, Integer page, Integer id) {
        Page<CategoryDO> pager = new Page<>(page, count);
        QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(CategoryDO::getIsRoot, CategoryRootOrNotEnum.NOT_ROOT.getValue());
        wrapper.lambda().eq(CategoryDO::getParentId, id); // 查parent_Id = 传入的id
        return this.getBaseMapper().selectPage(pager, wrapper);
    }

    // 取值
    public List<CategoryWithChildBO> getCategoryGroup() {
        // 所有节点
        List<CategoryWithChildBO> rootList = categoryMapper.getAllCategory();
        List<CategoryWithChildBO> categoryList = new ArrayList<>();

        // 区分父级和子级
        rootList.forEach(data->{
            if(data.getIsRoot()==CategoryRootOrNotEnum.ROOT.getValue()){
                categoryList.add(data);
            }
            data.setChildList(getCategoryTree(data.getId(),rootList));
        });

        return categoryList;
    }

    // 递归建树
    public List<CategoryWithChildBO> getCategoryTree(Long Id, List<CategoryWithChildBO> dataList) {
        Integer cId = Id.intValue();
        // 子节点
        List<CategoryWithChildBO> childList = new ArrayList<>();

        dataList.forEach(data->{
            if(cId.equals(data.getParentId())) {
                childList.add(data); // 子级遍历嵌套
                data.setChildList(getCategoryTree(data.getId(),dataList)); // 子级循环遍历嵌套
            }
        });

        if(dataList.size()==0){
            return null;
        }

        return childList;
    }


}
